﻿using System;
using System.Collections;

namespace DataScripter.Containers
{
    public abstract class ChildContainer : IContainer
    {
        private IContainer Container { get; set; }
        public string Name { get; private set; }
        private IContainer Parent { get; set; }

/*
        protected ChildContainer(IContainer parent) : this(typeof(ChildContainer).Name, parent)
        {
        }
*/

        protected ChildContainer(string name, IContainer parent)
        {
            Name = name;
            Container = parent.CreateChildContainer(Name);
            Parent = parent;
        }

        public void Initialize()
        {
            OnInitialize();
        }

        protected virtual void OnInitialize()
        {
        }

        public bool HasRegistration<T>()
        {
            return Container.HasRegistration<T>();
        }

        public void RegisterType<T>(Type type)
        {
            Container.RegisterType<T>(type);
        }

        public void RegisterType<T>(string name, Type type)
        {
            Container.RegisterType<T>(name, type);
        }

        public void RegisterType(string name, Type type, Type interfaceType)
        {
            Container.RegisterType(name, type, interfaceType);
        }

        public T Resolve<T>()
        {
            if (HasRegistration<T>()) 
                return Container.Resolve<T>();
            return Parent.Resolve<T>();
        }

        public T Resolve<T>(string name)
        {
            if (HasRegistration<T>(name))
                return Container.Resolve<T>(name);
            return Parent.Resolve<T>(name);
        }

        public void RegisterType<T>(T instance)
        {
            Container.RegisterType(instance);
        }

        public void RegisterType<T>(string name, T instance)
        {
            Container.RegisterType(name, instance);
        }

        public IContainer CreateChildContainer(string name)
        {
            return Container.CreateChildContainer(name);
        }

        public bool HasRegistration<T>(string name)
        {
            return Container.HasRegistration<T>(name);
        }

        public T Resolve<T>(string name, IDictionary dictionary)
        {
            return Container.Resolve<T>(name, dictionary);
        }
    }
}
